package com.example.gzysmartcity;

import android.annotation.SuppressLint;
import android.app.Application;

import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.example.gzysmartcity.repository.model.DaoMaster;
import com.example.gzysmartcity.repository.model.DaoSession;
import com.example.gzysmartcity.repository.model.MyUserDao;
import com.example.gzysmartcity.repository.model.OpinionDao;
import com.example.gzysmartcity.repository.model.SearchListDao;
import com.example.gzysmartcity.repository.model.SellCarDao;

public class App extends Application {
    private static OpinionDao opinionDao;
    private static MyUserDao myUserDao;
    private static SellCarDao sellCarDao;
    private static SearchListDao searchListDao;
    @SuppressLint("StaticFieldLeak")
    private static LocationClient mLocationClient = null;
    private final MyLocationListener myListener = new MyLocationListener();

    @Override
    public void onCreate() {
        super.onCreate();
        DaoSession opinionDaoSession = daoMaster("opinion.db");
        DaoSession opinionDaoSession2 = daoMaster("user.db");
        DaoSession sellCarDaoSession = daoMaster("sell.db");
        DaoSession searchDaoSession = daoMaster("search.db");
        opinionDao = opinionDaoSession.getOpinionDao();
        myUserDao = opinionDaoSession2.getMyUserDao();
        sellCarDao = sellCarDaoSession.getSellCarDao();
        searchListDao = searchDaoSession.getSearchListDao();
        //
        mLocationClient = new LocationClient(getApplicationContext());
        //声明LocationClient类
        mLocationClient.registerLocationListener(myListener);
        //注册监听函数
        LocationClientOption option = new LocationClientOption();
        option.setIsNeedAddress(true);
//可选，是否需要地址信息，默认为不需要，即参数为false
//如果开发者需要获得当前点的地址信息，此处必须为true
        option.setNeedNewVersionRgc(true);
//可选，设置是否需要最新版本的地址信息。默认需要，即参数为true
        mLocationClient.setLocOption(option);
//mLocationClient为第二步初始化过的LocationClient对象
//需将配置好的LocationClientOption对象，通过setLocOption方法传递给LocationClient对象使用
//更多LocationClientOption的配置，请参照类参考中LocationClientOption类的详细说明
        //
    }

    public static void start() {
        if (mLocationClient != null) {
            mLocationClient.start();
        }
    }
    public static void stop(){
        if (mLocationClient != null) {
            mLocationClient.stop();
        }
    }
    public static SearchListDao getSearchListDao() {
        return searchListDao;
    }

    public static OpinionDao getOpinionDao() {
        return opinionDao;
    }

    public static MyUserDao getMyUserDao() {
        return myUserDao;
    }

    public static SellCarDao getSellCarDao() {
        return sellCarDao;
    }

    private DaoSession daoMaster(String name) {
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this, name);
        return new DaoMaster(devOpenHelper.getWritableDatabase()).newSession();
    }
}
